package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/*
类简介：

功能：

时间：2020/4/15

*/
@RestController
@RequestMapping("order")
public class OrderController {

    @Reference
    private OrderService orderService;

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody Map<String,Object> pagination){

        QueryPageBean queryPageBean = new QueryPageBean();
        queryPageBean.setCurrentPage(Integer.parseInt(pagination.get("currentPage").toString()));
        queryPageBean.setPageSize(Integer.parseInt(pagination.get("pageSize").toString()));

        return orderService.findPage(queryPageBean,pagination);
    }

    @RequestMapping("findById")
    public Result findById(int id){
        Map<String, Object> order ;
        try {
            order = orderService.findByIdForEdit(id);
            return new Result(true,"成功",order);
        }catch (Exception e){
            return new Result(false, "失败",e.getMessage());
        }
    }

    @RequestMapping("/add")
    private Result add(@RequestBody Map<String,Object> order, Integer[] setmealIds){

        try {
            orderService.add(order,setmealIds);
            return new Result(true,"新增成功");
        }catch (Exception e){
            return new Result(false, "失败",e.getMessage());
        }
    }

    @RequestMapping("/deleteById")
    private Result deleteById(int id){
        try {
            orderService.deleteById(id);
            return new Result(true,"新增成功");
        }catch (Exception e){
            return new Result(false, "失败",e.getMessage());
        }
    }

    @RequestMapping("/editVisitedStatus")
    private Result editVisitedStatus(@RequestBody Map<String ,Object> map){
        try {
            orderService.editVisitedStatus((int)map.get("id"),map.get("status").toString());
            return new Result(true,"新增成功");
        }catch (Exception e){
            return new Result(false, "失败",e.getMessage());
        }
    }

    @RequestMapping("/edit")
    private Result edit(@RequestBody Order order, Integer[] setmealIds){
        for (Integer setmealId : setmealIds) {
            System.out.println("setmealId = " + setmealId);
        }

        try {
            orderService.edit(order,setmealIds);
            return new Result(true,"修改成功");
        }catch (Exception e){
            return new Result(false, "失败",e.getMessage());
        }
    }

}
